package com.example.bootdemo.service;

import com.example.bootdemo.domain.JwtUserDto;
import com.example.bootdemo.domain.MyRole;
import com.example.bootdemo.domain.MyUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class UserDetailService implements UserDetailsService {

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private UserService userService;

/*    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 模拟一个用户，替代数据库获取逻辑
        MyUser user = new MyUser();
        user.setUserName(username);
        user.setPassword(this.passwordEncoder.encode("123456"));
        // 输出加密后的密码
        System.out.println(user.getPassword());

        List<GrantedAuthority> authorities = new ArrayList<>();
        if (StringUtils.equalsIgnoreCase("mrbird", username)) {
            authorities = AuthorityUtils.commaSeparatedStringToAuthorityList("admin");
        } else {
            authorities = AuthorityUtils.commaSeparatedStringToAuthorityList("test");
        }
        return new User(username, user.getPassword(), user.isEnabled(),
                user.isAccountNonExpired(), user.isCredentialsNonExpired(),
                user.isAccountNonLocked(), authorities);
    }*/



   // @Override
    public UserDetails loadUserByUsername1(String userName){
        // 根据用户名获取用户
        com.example.bootdemo.entity.MyUser user1 = userService.getUserByName(userName);
        if (user1 == null ){
            throw new BadCredentialsException("用户名或密码错误");
        }
        MyUser user = new MyUser();
        user.setUserName(userName);
        user.setPassword(this.passwordEncoder.encode("123456"));
        // 输出加密后的密码
        System.out.println(user.getPassword());

        List<GrantedAuthority> authorities = new ArrayList<>();
        if (StringUtils.equalsIgnoreCase("mrbird", userName)) {
            authorities = AuthorityUtils.commaSeparatedStringToAuthorityList("admin");
        } else {
            authorities = AuthorityUtils.commaSeparatedStringToAuthorityList(userName);
        }
        return new User(userName, user.getPassword(), user.isEnabled(),
                user.isAccountNonExpired(), user.isCredentialsNonExpired(),
                user.isAccountNonLocked(), authorities);
    }

    @Override
    public JwtUserDto loadUserByUsername(String userName){
        // 根据用户名获取用户

        com.example.bootdemo.entity.MyUser user1 = userService.getUserByName(userName);
        if (user1 == null ){
            throw new BadCredentialsException("用户名或密码错误");
        }
        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
      //  List<MenuIndexDto> list = menuDao.listByUserId(user.getUserId());
      //  List<String> collect = list.stream().map(MenuIndexDto::getPermission).collect(Collectors.toList());
//        for (String authority : collect){
//            if (!("").equals(authority) & authority !=null){
//                GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(authority);
//                grantedAuthorities.add(grantedAuthority);
//            }
//        }
        //将用户所拥有的权限加入GrantedAuthority集合中
        MyUser myUser=new MyUser();
        myUser.setUserName(userName);

        myUser.setPassword(this.passwordEncoder.encode("123456"));
        JwtUserDto loginUser =new JwtUserDto(myUser,grantedAuthorities);
        List <MyRole> roleList = new ArrayList<>();
        MyRole myRole=new MyRole(111);
        roleList.add(myRole);
        loginUser.setRoleInfo(roleList);
        return loginUser;
    }


}
